package cn.edu.scujcc.customofsichuan;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import cn.edu.scujcc.customofsichuan.Dao.UserDao;
import cn.edu.scujcc.customofsichuan.DataBase.UserDatabase;
import cn.edu.scujcc.customofsichuan.Entity.User;

public class Rigister extends AppCompatActivity {

    private EditText rigister_confirm_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rigister);
        //获取数据库
        UserDatabase userDatabase=UserDatabase.getInstance(this);
        UserDao userDao = userDatabase.getUserDao();


        ImageView rigister_back = findViewById(R.id.rigister_back);
        EditText rigister_name = findViewById(R.id.rigister_name);
        EditText rigister_account = findViewById(R.id.rigister_account);
        EditText rigister_password = findViewById(R.id.rigister_password);
        Button btn_rigister = findViewById(R.id.btn_rigister);
        rigister_confirm_password = findViewById(R.id.rigister_confirm_password);



        //添加监听事件
        rigister_confirm_password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {

            }
        });



        rigister_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        btn_rigister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = rigister_name.getText().toString().trim();
                String account = rigister_account.getText().toString().trim();
                String password = rigister_password.getText().toString().trim();
                String confirm_password = rigister_confirm_password.getText().toString().trim();

                if (!name.equals("")&&!account.equals("")&&!password.equals("")&&!confirm_password.equals("")){
                        if (password.equals(confirm_password)){

                            User user = new User(null, name, account, password, null);
                            new InsertUserTask(userDao).execute(user);

                            Toast.makeText(Rigister.this, "注册成功！3秒后跳转至登陆界面", Toast.LENGTH_LONG).show();
                            try {
                                Thread.sleep(3000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            Intent intent=new Intent(v.getContext(),login.class);
                            startActivity(intent);

                        }else {
                            Toast.makeText(Rigister.this, "两次密码不一致，请再次确认密码！", Toast.LENGTH_SHORT).show();
                            rigister_confirm_password.requestFocus();
                        }

                }else {
                     Toast.makeText(Rigister.this, "以上内容不能为空！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


    //由于查询数据库是耗时工作一般放在后台运行，如果放在主线程中会导致界面卡顿，并且Room框架也不允许放于主线程中运行，故新建一个内部类
    class InsertUserTask extends AsyncTask<User,Void,Void>{
        private UserDao userDao;
        public InsertUserTask(UserDao userDao) {
            this.userDao = userDao;
        }
        @Override
        protected Void doInBackground(User... users) {
            userDao.insertUser(users);
            return null;
        }
    }

}